/*
* Utility.cpp
*
*  Created on: 2013-10-16
*      Author: WinDDK
*/

#include "Utility.h"

void * __cdecl operator new(size_t size, POOL_TYPE type)
{
    void *ptr = ExAllocatePoolWithTag(type, size, UTILITYTAG);
    if (ptr != NULL)
    {
        RtlZeroMemory(ptr, size);
    }
    return ptr;
}

void * __cdecl operator new(size_t size, POOL_TYPE type, ULONG tag=UTILITYTAG)
{
    void *ptr = ExAllocatePoolWithTag(type, size, tag);
    if (ptr != NULL)
    {
        RtlZeroMemory(ptr, size);
    }
    return ptr;
}

void __cdecl operator delete(void* ptr)
{
    if (ptr != NULL)
    {
        ExFreePool(ptr);
    }
}

void __cdecl operator delete[](void* ptr)
{
    if (ptr != NULL)
    {
        ExFreePool(ptr);
    }
}

void __cdecl PageProtectOff()
{
    _asm
    {
        cli
        push eax
        mov eax, cr0
        and eax, not 10000h
        mov cr0, eax
        pop eax
    }
}

void __cdecl PageProtectOn()
{
    _asm
    {
        push eax
        mov eax, cr0
        or eax, 10000h
        mov cr0, eax
        pop eax
        sti
    }
}

ULONG __cdecl GetFunctionAddressByName(IN PCWSTR functionName)
{
    UNICODE_STRING uniCodeFunctionName;
    RtlInitUnicodeString(&uniCodeFunctionName, functionName);
    return (ULONG) MmGetSystemRoutineAddress(&uniCodeFunctionName);
}
